﻿<Window x:Class="_12.BindToList.SelectValue"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:_12.BindToList"
        xmlns:p11="clr-namespace:_11.DataBinding;assembly=11.DataBinding"
        Title="SelectValue" Height="243" Width="300">
    <Window.Resources>
        <local:PersonCollection x:Key="persons">
            <p11:Person SSN="100" Name="Henry"/>
            <p11:Person SSN="200" Name="Mary"/>
            <p11:Person SSN="300" Name="Alice"/>
        </local:PersonCollection>
        <Style TargetType="ListBox">
            <Setter Property="Margin" Value="5"/>
        </Style>
        <Style TargetType="Label">
            <Setter Property="Margin" Value="5"/>
        </Style>
        <Style TargetType="ComboBox">
            <Setter Property="Margin" Value="5"/>
        </Style>
    </Window.Resources>
    <Grid DataContext="{StaticResource persons}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <ListBox Name="lbxPersons"
                 ItemsSource="{Binding}"
                 DisplayMemberPath="Name"
                 SelectedValuePath="SSN"
                 IsSynchronizedWithCurrentItem="True"
                 MouseDoubleClick="lbxPersons_MouseDoubleClick"/>
        <StackPanel Grid.Column="1">
            <Label  Name="lblSSN" 
                    HorizontalContentAlignment="Center"
                    Content="{Binding Path=SSN}"
                    BorderBrush="orange"
                    BorderThickness="1"/>
            <!--chekatodo: !!! Pay much attention here, if binding to 'SelectedValue' 
            without explicitly assigning 'Mode=OneWay', there is possiblity
            that when change the selection in combox, it will modify the value of 
            background current item-->
            <ComboBox  Name="cbxPersons" 
                       ItemsSource="{Binding}"
                       DisplayMemberPath="Name"
                       SelectedValuePath="SSN"
                       SelectedValue="{Binding Path=SSN, Mode=OneWay}"
                       IsSynchronizedWithCurrentItem="True"/>
            <Button Content="Inspect" Name="btnInspect" Margin="5"
                    Click="btnInspect_Click" />
        </StackPanel>
    </Grid>
</Window>
